<!-- 智能体管理 -->
<template>
  <div class="children_content">
    <div class="add_btns_box">
      <div class="box_left">
        <div class="title">智能体管理</div>
      </div>
      <div class="box_right">
        <Space>
          <Button type="primary" @click="showAdd = true" icon="md-add">新增</Button>
          <Button type="error" icon="ios-trash" @click="batchDel">批量删除</Button>
          <Button type="primary" ghost icon="md-cloud-download">导出</Button>
          <Button type="primary" ghost icon="md-cloud-upload">导入</Button>
        </Space>
      </div>
    </div>
    <div class="content_table">
      <vxe-table ref="tableRef" :data="tableData" border="none" sync-resize size="small" stripe
        :min-height="50" class="right_table" :max-height="tableHeight + 'px'" :loading="loading">
        <vxe-column type="checkbox" width="50" align="center" fixed="left"></vxe-column>
        <vxe-column title="智能体名称" field="name"></vxe-column>
        <vxe-column title="类型" field="type"></vxe-column>
        <vxe-column title="模型" field="model"></vxe-column>
        <vxe-column title="状态" field="status">
          <template #default="{ row }">
            <Tag v-if="row.status === 1" color="success">运行中</Tag>
            <Tag v-else-if="row.status === 0" color="warning">已停止</Tag>
            <Tag v-else color="error">异常</Tag>
          </template>
        </vxe-column>
        <vxe-column title="创建时间" field="createTime"></vxe-column>
        <vxe-column title="操作" fixed="right" width="230" align="center">
          <template #default="{ row }">
            <div class="table_btns">
              <text-btn @click="handleEdit(row)" />
              <text-btn type="del" @click="handleDelete(row)" />
              <text-btn :text="row.status === 1 ? '停止' : '启动'" @click="toggleStatus(row)" />
              <text-btn text="调试" @click="handleDebug(row)" />
            </div>
          </template>
        </vxe-column>
      </vxe-table>
      <div class="page_box">
        <Page v-model="pageIndex" :page-size="pageSize" :total="total" show-elevator show-total
          @on-change="handlePageChange" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance, inject, watch } from 'vue'
import { Message } from 'view-ui-plus'

// 当前对象
const { proxy } = getCurrentInstance()

// 屏幕高度
const screenHeight = inject('screenHeight')
// 表格高度
const tableHeight = ref(500)
// 监听 screenHeight 的变化
watch(
  screenHeight,
  newHeight => {
    tableHeight.value = newHeight - 245
  },
  { immediate: true }
)

// 是否显示新增
const showAdd = ref(false)

// 分页相关
const pageIndex = ref(1)
const pageSize = 20
const total = ref(0)

// 加载状态
const loading = ref(false)

// 列表数据
const tableData = ref([
  { name: '客服智能体', type: 'LLM', model: 'GPT-4', status: 1, createTime: '2023-01-01' },
  { name: '数据分析智能体', type: 'LLM', model: 'Claude-3', status: 1, createTime: '2023-01-02' },
  { name: '风控智能体', type: '规则引擎', model: '自定义', status: 0, createTime: '2023-01-03' }
])

// 批量删除
const batchDel = () => {
  proxy.$Modal.confirm({
    mask: false,
    title: '警告',
    content: '<p>是否批量删除选中数据?</p>',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      Message.success('批量删除成功')
    }
  })
}

// 编辑
const handleEdit = (row) => {
  showAdd.value = true
  // 这里可以添加编辑逻辑
}

// 删除
const handleDelete = (row) => {
  proxy.$Modal.confirm({
    mask: false,
    title: '警告',
    content: '<p>是否删除该数据?</p>',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      Message.success('删除成功')
    }
  })
}

// 切换状态
const toggleStatus = (row) => {
  loading.value = true
  setTimeout(() => {
    row.status = row.status === 1 ? 0 : 1
    loading.value = false
    Message.success(row.status === 1 ? '启动成功' : '停止成功')
  }, 1000)
}

// 调试智能体
const handleDebug = (row) => {
  Message.info(`正在打开调试界面: ${row.name}`)
  // 这里可以添加打开调试界面的逻辑
}

// 页码变化
const handlePageChange = (page) => {
  pageIndex.value = page
  // 这里可以添加获取数据的逻辑
}

onMounted(() => {
  // 这里可以添加初始化逻辑
})
</script>

<style lang="scss" scoped></style>
